package com.front.frontdemo.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统运行需要的必备操作在此filter内进行
 * @author zhangrb
 *
 */
public class InitFilter implements Filter {
	
	private String[] excludes;
	
	public void init(FilterConfig filterConfig) throws ServletException {
		excludes = new String[]{".jpg,.png,.gif,.bmp,.doc,.xls,.docx,.xlsx,.ppt,.pptx,.js,.css,.avi,.mp4,.apk"};
		Map<String, Object> m = new HashMap<>();
		m.put("ads", 12);
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		XssHttpServletRequestWraper xssRequest = new XssHttpServletRequestWraper((HttpServletRequest) request);
		//若当前session中存在账号，则将账号对应的操作机构也保存至session,方便以后查询
		HttpServletRequest req = (HttpServletRequest)xssRequest;
//		User user = (User)req.getSession().getAttribute(SessionInfo.USER);
//		if(user == null) {
//			log.debug("用户未登录!!!!!!");
//			chain.doFilter(req, response);
//			return;
//		}
		String uri = req.getRequestURI();
		for(String subfix : excludes) {
			if(uri.equalsIgnoreCase(subfix)) {
				chain.doFilter(req, response);
				return;
			}
		}
		HttpSession s = req.getSession();

		chain.doFilter(req, response);
		return;
	}

	public void destroy() {
	}

}
